<?php
/**
 *	品牌
 *
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');

class brandControl extends MobileControl{

    public function __construct(){
		parent::__construct();
    }
	//品牌搜索列表页
    public function indexOp(){
		$brand_model =new brandModel();
		$brand_class_model =new brand_classModel();
		//分页
		$page = new Page();
		$page->setEachNum(40);
		$page->setStyle('admin');
		
		//按搜索关键词筛选(form提交)
        if(isset($_GET['keyword']) && trim($_GET['keyword']) != ''){
            $param['brand_name'] = $_GET['keyword'];
        }
		
		//根据会员填写的地区信息进行判断展示哪个区域的品牌
		if(!(int)$_GET['prov_id']>0 && !(int)$_GET['city_id']>0){
			if(isset($_SESSION['is_login']) && (int)$_SESSION['is_login'] == 1){
				$member = Model('member')->infoMember(array('member_id'=>(int)$_SESSION['member_id']));
				$area_info = preg_split('/\t/', $member['member_areainfo'],-1,PREG_SPLIT_NO_EMPTY);
				if(count($area_info)==1){
					$param['show_in_province_ids'] = (int)$member['member_provinceid'];
				}elseif(count($area_info)==2 || count($area_info)==3){
					$param['show_in_city_ids'] = (int)$member['member_cityid'];
				}
			}
		}
		  //品牌列表数据
        if((int)$_GET['bc_id']>0){$param['bc_id'] = $_GET['bc_id'];}
        $param['order'] = 'brand_sort desc,brand_rank desc,brand_id desc';
		$brand_list =array();
        $brand_list = $brand_model->getBrandList($param,$page);
		$this->_assignMobileData($page);
		Tpl::output('brand_list',$brand_list);

        // 品牌分类列表
        $brand_class = $brand_class_model->getClassList ( array () );
        //Tpl::output('brand_class_list',$brand_class);

        //按类型组合分类 Start
        $sql = "SELECT * FROM #__brand_class_type ORDER BY type_sort ASC,type_id ASC ";
        $type = Db::queryAll($sql);
        $type_class = array();
        if(!empty($type) && is_array($type)){
            foreach ($type as $k=>$v){
                if(!empty($brand_class) && is_array($brand_class)){
                    foreach ($brand_class as $key=>$val){
                        if($val['type_id'] == $v['type_id']){
                            $type_class[$v['type_id']][] = $val;
                        }
                    }
                }
            }
        }
        if(!empty($brand_class) && is_array($brand_class)){
            foreach ($brand_class as $key=>$val){
                if($val['type_id'] == 0){
                    $type_class[$val['type_id']][] = $val;
                }
            }
        }
       /*  echo "<pre>";
        var_dump($type_class);exit;*/
        Tpl::output('type_class',$type_class);
        //按类型组合分类 End

        Tpl::showpage('brand_list');
    }
		
	//品牌商品列表
	public function  brand_goodsOp(){
		$brand_id = ( int ) $_GET ['brand_id'];
		$brand_model = new brandModel ();
		$brand_info = $brand_model->getOneBrand ( $brand_id );
		Tpl::output ( 'brand_info', $brand_info );
		$brand_store_model = new brand_storeModel ();
		$brand_store_info = $brand_store_model->getBrandStoreInfo ( $brand_info ['brand_store_id'] );
		if ($brand_store_info ['brand_store_state'] == 0 || (intval ( $brand_store_info ['brand_store_end_time'] ) != 0 && $brand_store_info ['brand_store_end_time'] <= time ())) {
			appMessage ( '您要查看的商城不存在或正在审核中');
		}
		$brand_store_info ['brandCount'] = $brand_model->getBrandCount ( 'brand', array (
				'brand_store_id' => $brand_info ['brand_store_id'] 
		) );
		Tpl::output ( 'brand_store_info', $brand_store_info );
		
		$sql = "SELECT * FROM #__goods WHERE brand_id = '$brand_id' AND goods_show = 1 AND goods_state = 0";
		$pageSize=25;
		$curPage=$_GET['curpage'];
		$rs=db::PageQuery($sql,$curPage,$pageSize);
		$list=$rs->fetchAll();
		$page = new Page();
		$page->setEachNum($pageSize);
		$page->setNowPage($curPage);
		$page->setTotalNum($rs->pageRows());
		$page->setStyle('admin');
		
		$this->_assignMobileData($page);
		Tpl::output('list',$list);
		Tpl::showpage ( 'brand_goods');
	}
		
	//品牌详情页
	public function brand_detailOp(){
		$brand_id = (int)$_GET['brand_id'];
		
		$brand_model =new brandModel();
		$brand_info = $brand_model->getOneBrand($brand_id);
		$brand_body = $brand_model->getBrandBody($brand_id);
		
		$brand_store_model =new brand_storeModel();
		
		$brand_store_info = $brand_store_model->getBrandStoreInfo($brand_info['brand_store_id']);

        if($brand_store_info['brand_store_state'] == 0 || (intval($brand_store_info['brand_store_end_time']) != 0 && $brand_store_info['brand_store_end_time'] <= time())){
            appMessage('您要查看的商城不存在或正在审核中');
        }
        $brand_store_info['brandCount'] = $brand_model->getBrandCount('brand',array('brand_store_id'=>$brand_info['brand_store_id']));

		$brand_comment_model =new brand_commentModel();
		
		$comment_list = $brand_comment_model->getCommentList($brand_id);
		if(is_array($comment_list)){
                      foreach ($comment_list as $k=>$v){
                          $sql = "SELECT * FROM #__brand_comment_pic WHERE comment_id = '{$v['comment_id']}'";
                          $comment_list[$k]['pic_list'] = Db::queryAll($sql);
                      }
                  }
                  
		$member_model =new memberModel();
		if(is_array($comment_list) && !empty($comment_list)){
			foreach($comment_list as $key=>$val){
				$info = $member_model->infoMember(array('member_id'=>$val['comment_memberid']));
				$comment_list[$key]['member_avatar'] = $info['member_avatar'];
			}
		}
        
                //品牌加盟区域地图
		$sql = "SELECT * FROM #__brand_alliance_setting WHERE brand_id = '$brand_id'";
		$brand_map = Db::queryRow($sql);
		Tpl::output('brand_map', $brand_map);
        
                 //品牌商品列表
                $sql = "SELECT * FROM #__goods WHERE brand_id = '$brand_id' AND goods_show = 1 AND goods_state = 0  limit 20";
                $brand_goods_list=Db::queryAll($sql);
                Tpl::output('brand_goods_list',$brand_goods_list);
		
		//$store_id = $member_model->infoMember(array('brand_store_id'=>$brand_info['brand_store_id']));
		//$store_info = $this->getStoreInfo($store_id['store_id']);
		Tpl::output('html_title',$brand_info['brand_name'].'_'.C('site_name'));
		Tpl::output('seo_keywords',$brand_info['brand_seo_keywords']);
		Tpl::output('seo_description',$brand_info['brand_seo_description']);
		Tpl::output('store_info',$store_info);
		Tpl::output('comment_list',$comment_list);
		Tpl::output('nchash',substr(md5(SiteUrl.$_GET['act'].$_GET['op']),0,8));
		Tpl::output('brand_store_info',$brand_store_info);
		Tpl::output('brand_body',$brand_body);
		Tpl::output('brand_info',$brand_info);
		Tpl::output('brand_id',$brand_id);
		if(!cookie('DEX_IS_ACCESS')){
			setNcCookie('DEX_IS_ACCESS',1);// 写到brand_store_hits
			$sql="SELECT brand_hits FROM #__brand WHERE brand_id='$brand_id'";
			$hits=db::queryOne($sql);
			if($hits==0){$hits=1085;}
			$hits++;
			db::update('brand',array('brand_hits'=>$hits),"brand_id='$brand_id'");
		}else
		{
			$sql="SELECT brand_hits FROM #__brand WHERE brand_id='$brand_id'";
			$hits=db::queryOne($sql);
			setNcCookie('DEX_IS_ACCESS',$hits);
		}

		//是否收藏过该品牌
		$fav_id = intval($_GET['brand_id']);
		if ($fav_id > 0) {
			$sql = "SELECT * FROM #__brand_favorites WHERE member_id = '{$_SESSION['member_id']}' AND brand_fav_id = '$fav_id'";
			$favorites_info = Db::queryRow($sql);
			$is_fal = false;
			if (!empty($favorites_info)) {
				$is_fal  = true;
			}
			Tpl::output('is_fal',$is_fal);
		}

		//当前商城收藏会员ID
		$sql = "SELECT member_id FROM #__brand_favorites WHERE brand_fav_id = '$brand_id'";
		$rs = Db::queryAll($sql);
		$m_id = array();
		foreach($rs as $item){
			$m_id[] = $item['member_id'];
		}
		Tpl::output('m_id',$m_id);
		
		Tpl::output('hits',$hits);
		if($brand_id==156){ //156,53
			$this->loadMeicaiMedia('ys');
			Tpl::showpage('ys_brand_detail');
		}elseif($brand_id==53){
			$this->loadMeicaiMedia('dex');
			Tpl::showpage('dex_brand_detail');
		}else{
			Tpl::showpage('brand_detail');
		}
	}
    
        //留言
        public function feedbackOp() {
            if ($_SESSION ['brand_store_id'] == $_POST ['brand_store_id']) {
                appJump ( '不能给自己的商城留言' );
            }
            $feedback_model = new brand_store_feedbackModel ();
            $obj_validate = new Validate ();
            $obj_validate->validateparam = array (
                    array ( "input" => $_POST ["feedback_name"], "require" => "true", "message" => '姓名不能为空'  ),
                    array ( "input" => $_POST ["feedback_phone"], "require" => "true", "message" => '手机不能为空' ),
                    array ( "input" => $_POST ["feedback_qq"], "require" => "true", "message" => 'QQ不能为空' ),
                    array ( "input" => $_POST ["feedback_address"], "require" => "true", "message" => '地址不能为空'),
                    array ( "input" => $_POST ["feedback_content"], "require" => "true", "message" => '留言内容不能为空') 
            );
            $error = $obj_validate->validate ();
            if ($error != '') {
                appJump ( $error );
            }
            $_POST ['add_time'] = time ();
            if ($feedback_model->insertFeedback ( $_POST )) {
                appJump ( '留言成功', "index.php?act=brand&op=brand_detail&brand_id={$_POST['brand_id']}" );
            } else {
                appJump ( '留言失败' );
            }
        }
        
        // 评论提交
        public function commentOp() {
            if ($_POST ['brand_store_id'] == $_SESSION ['brand_store_id']) {
                appMessage ( '不能评论自己的品牌');
            }
            if (! isset ( $_SESSION ['is_login'] ) || $_SESSION ['is_login'] != 1) {
                appMessage ( '请先登陆', "index.php?act=login" );
            }
            $obj_validate = new Validate ();
            $obj_validate->validateparam = array (
                    array (
                            "input" => $_POST ["comment"],
                            "require" => "true",
                            "message" => '评论内容不能为空' 
                    ) 
            );
            $error = $obj_validate->validate ();
            if ($error != '') {
                appMessage ( $error );
            }
            if (! checkSeccode ( $_POST ['nchash'], $_POST ['captcha'] )) {
                appMessage ( '验证码输入错误' );
            }
           
            $brand_comment_model = new brand_commentModel ();
            $res = $brand_comment_model->insertComment ( $_POST );
            if ($res) {
                $upload = new UploadFile ();
                $img_save_path = ATTACH_BRAND . '/comment/'.$upload->getSysSetPath();
                $upload->set ( 'default_dir', $img_save_path );
                $upload->set ( 'allow_type', array('jpg','jpeg','png','gif' ));
                $upload->set ( 'max_size', C ( 'image_max_filesize' ) );
                $upload->set ( 'thumb_width', 60 );
                $upload->set ( 'thumb_height', 60 );
                $upload->set ( 'thumb_ext', '_60X60' );

                $img_array = array();
                $item = 0;
                for($i=1;$i<=5;$i++){
                    if($_FILES['comment_pic_'.$i]['name'] != ''){
                        if($upload->upfile ( 'comment_pic_'.$i )){
                            $img_array[$item]['thumb_img_path'] = $img_save_path.$upload->thumb_image;
                            $img_array[$item]['img_path'] = $img_save_path.$upload->file_name;
                            $img_array[$item]['comment_id'] = $res;
                            $item ++ ;
                        }
                    }
                } 
                if(count($img_array) > 0){
                    Db::insertAll('brand_comment_pic',$img_array);
                }
                
                appMessage ( '评论成功', "index.php?act=brand&op=brand_detail&brand_id={$_POST['brand_id']}" );
            } else {
                appMessage ( '评论失败' );
            }
        }
	
	//加载御神 戴尔熙 媒体列表
    private function loadMeicaiMedia($brand){
		$video_url = 'http://media.mrcysxy.com';
		if($brand == 'ys'){
			$media_path_cous = 'ysjf';	//客户见证
			$media_path_boss = 'jmd/ys';		//老板见证
			$cous_arr = array();
			$boss_arr = array();
			for($i=0;$i<92;$i++){
				$cous_arr[$i]['mvUrl'] = $video_url.'/media/'.$media_path_cous.'/'.($i+1);
				$cous_arr[$i]['imgUrl'] = SiteUrl.'/media/'.$media_path_cous.'/'.($i+1);
			}
			for($i=0;$i<16;$i++){
				$boss_arr[$i]['mvUrl'] = $video_url.'/media/'.$media_path_boss.'/'.($i+1);
				$boss_arr[$i]['imgUrl'] = SiteUrl.'/media/'.$media_path_boss.'/'.($i+1);
			}
			Tpl::output('cous_arr',$cous_arr);
			Tpl::output('boss_arr',$boss_arr);
		}
		
		if($brand == 'dex'){
			$media_path_cous_hm = 'dex/dexhm';
			$media_path_cous_ys = 'dex/dexhm';
			$media_path_boss = 'jmd/dex';
			$cous_hm_arr = array();
			$cous_ys_arr = array();
			$boss_arr = array();
			for($i=0;$i<132;$i++){
				$cous_hm_arr[$i]['mvUrl'] = $video_url.'/media/'.$media_path_cous_hm.'/'.($i+1);
				$cous_hm_arr[$i]['imgUrl'] = SiteUrl.'/media/'.$media_path_cous_hm.'/'.($i+1);
			}
			for($i=0;$i<14;$i++){
				$cous_ys_arr[$i]['mvUrl'] = $video_url.'/media/'.$media_path_cous_ys.'/'.($i+1);
				$cous_ys_arr[$i]['imgUrl'] = SiteUrl.'/media/'.$media_path_cous_ys.'/'.($i+1);
			}
			for($i=0;$i<17;$i++){
				$boss_arr[$i]['mvUrl'] = $video_url.'/media/'.$media_path_boss.'/'.($i+1);
				$boss_arr[$i]['imgUrl'] = SiteUrl.'/media/'.$media_path_boss.'/'.($i+1);
			}
			Tpl::output('cous_hm_arr',$cous_hm_arr);
			Tpl::output('cous_ys_arr',$cous_ys_arr);
			Tpl::output('boss_arr',$boss_arr);
		}
    }
}